package com.example.szh.mvp.model;

import com.example.szh.Api;
import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.LoginEntity;
import com.example.szh.http.HttpCode;
import com.example.szh.http.HttpFactory;
import com.example.szh.http.HttpType;
import com.example.szh.mvp.contract.LoginContract;
import com.example.szh.nextwork.ChangeFunction;
import com.example.szh.nextwork.model.BaseModel;

import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.RequestBody;

public class LoginModel extends BaseModel implements LoginContract.ILoginModel {
    @Inject
    public LoginModel(){}
    @Override
    public Observable<BaseEntity> request(Map<String, Object> map) {
        //区分当前使用的接口请求
        int  code = (int) map.get("code");
        map.remove("code");
        if (code== HttpCode.LOGINCODE){
            //登录接口逻辑使用
            //map->JSON字符串->生成requestbody
            RequestBody body = createBody(map);
            //根据在api中配置对应的服务器json返回值生成的bean类
            Observable<LoginEntity> ob = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                    .getRetrofit()
                    .create(Api.class).requestLogin(body);
            //对异步任务进行转换
            return ob.map(new ChangeFunction<LoginEntity>());
        }else if (code==HttpCode.REGISTCODE){
            //注册接口逻辑使用
        }
        return null;
    }
}
